package com.chenxiao.mybatisplus.config;



import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: CX
 * @Date: Created in  2023/02/03 13:55
 * @version: v1.0
 * @Description: TODO
 **/
@Configuration
public class RabbitConfig {


    //队列 起名：OrderDirectQueue
    @Value("${rabbitConfig.queueName}")
    private String queueName;
    @Bean
    public Queue OrderDirectQueue() {
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue(queueName,true);
    }
    private String orderQueueName = "OrderQueue";
    @Bean
    public Queue OrderQueue(){
        return new Queue(orderQueueName, true);
    }

    //Direct交换机 起名：TestDirectExchange
    @Value("${rabbitConfig.exchageName}")
    private String exchageName;
    @Bean
    DirectExchange OrderDirectExchange() {
        //  return new DirectExchange("TestDirectExchange",true,true);
        return new DirectExchange(exchageName,true,false);
    }
    @Bean
    DirectExchange OrderExchange() {
        //  return new DirectExchange("TestDirectExchange",true,true);
        return new DirectExchange("OrderExchange",true,false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
    @Value("${rabbitConfig.directRoutingName}")
    private String directRoutingName;
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(OrderDirectQueue()).to(OrderDirectExchange()).with(directRoutingName);
    }
    @Bean
    Binding bindingDirect2(){
        return BindingBuilder.bind(OrderQueue()).to(OrderExchange()).with("OrderRouting");
    }
    @Bean
    public SimpleRabbitListenerContainerFactory getSimpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //使用jackson进行消息序列与反序列
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 开启手动 ack
        return factory;
    }

   // @Bean
    /*DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }*/
/*
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if(ack)
                    System.out.println("触发confirm回调函数,交换机接受到了");
                else{
                    System.out.println("触发confirm回调函数,交换机收不到信息,原因:"+cause);
                    System.out.println("消息对应的的CorrelationData id:"+correlationData.getId());
                }
            }
        });
        return rabbitTemplate;
    }*/


}
